package com.woniuxy.project.campus.account.controller;


import cn.hutool.json.JSONUtil;
import com.woniuxy.project.campus.account.dto.ResourceDto;
import com.woniuxy.project.campus.account.dto.SelectPermissionByRoleIdDto;
import com.woniuxy.project.campus.account.model.form.RedisKey;
import com.woniuxy.project.campus.account.redisClint.RedisUtil;
import com.woniuxy.project.campus.account.service.PermissionService;
import com.woniuxy.project.campus.util.Result;
import com.woniuxy.project.campus.util.TokenTool;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private PermissionService permissionService;
    //TODO:
    //根据id 查询前端权限
    @ApiOperation(value = "根据id 查询前端权限")
    @GetMapping("/selectPermissionByUserId")
    public Result selectPermissionByUserId(@RequestHeader("token") String token){
        //TODO: 根据userId查询权限列表
        Integer userId = TokenTool.getUserIdByToken(token);
        String json = redisUtil.getString(RedisKey.userResourceKey(userId));
        List<ResourceDto> list = JSONUtil.toList(JSONUtil.parseArray(json), ResourceDto.class);
        return Result.success(list);
    }
    //查询不同角色所对应的权限
    @ApiOperation(value = "查询不同角色所对应的权限")
    @GetMapping("/selectPermissionByRoleId")
    public Result selectPermissionByRoleId(@RequestHeader("token") String token){
        //TODO
        List<SelectPermissionByRoleIdDto> list =permissionService.selectPermissionByRoleId(token) ;
        return Result.success(list);
    }


    //

}

